package com.yt.ds.chapter2;

import com.yt.ds.util.SortUtil;

/**
 * @Author: lzf
 * @Date: 2020/7/10 17:10
 * @Description: 选择排序: 最小的换a[0],第二小的换a[1] 以此类推
 */
public class SelectionSort implements Sort {
    @Override
    public Comparable[] sort(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            int min = i;
            for (int k = i; k < a.length; k++) {
                if (a[k].compareTo(a[min]) < 0) {
                    min = k;
                }
            }
            SortUtil.exch(a, min, i);
        }
        return a;
    }
}
